package com.ruoyi.aitqjkj.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.aitcommon.utils.EntityCache;
import com.ruoyi.aitcommon.utils.ObjUtils;
import com.ruoyi.aitcommon.utils.RestServiceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.ruoyi.common.utils.sign.Md5Utils.toHex;
import static org.apache.commons.codec.digest.DigestUtils.md5;
import static org.apache.commons.lang.StringUtils.upperCase;

/**
 * 时代光华培训系统集成
 */
@Service
public class SDGHService {
    @Autowired
    IAitEntityInfoService infoService;
    @Autowired
    EntityCache entityCache;


    private String URL = null;
    private String URLName = null;
    private String requestURI = null;

    private void initParm(Map<String,Object> interParm) {
        this.URL = ObjUtils.toString(interParm.get("url"));
        this.URLName = ObjUtils.toString(interParm.get("URLName"));
        this.requestURI = ObjUtils.toString(interParm.get("requestURI"));
    }
    private String createSign(Map<String,Object> interParm) throws Exception {
        this.initParm(interParm);
        //固定获取sign_
        String appSecret = "DB0E4580DD9C4830A18749ED0C54A7A9";
        String signText = appSecret + "|" + requestURI + "|" + appSecret;
        String sign_ = upperCase(toHex(md5(signText)));
        if (sign_==null || sign_=="") {
            throw new Exception("获取Sign_失败!");
        }
        return sign_;
    }
    @ServiceLog("时代光华接口在线学习记录")
    public List<Map<String,Object>> getSDDates(Map<String,Object> interParm)throws Exception{
        //调用时代光华接口
        //企业id
        String appKey_ = "BB84A7C940564253B418F46DBBB04DE9";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建Calendar 的实例
        Calendar calendar = Calendar.getInstance();
        //当前时间减去一天，即一天前的时间
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        //得到前一天的时间
        Date dBefore = calendar.getTime();
        //格式化前一天
        String defaultStartDate = dateFormat.format(dBefore) ;
        defaultStartDate = defaultStartDate.substring(0,10) + " 00:00:00";
        String defaultEndDate = defaultStartDate.substring(0,10) + " 23:59:59";
        //前一天0点
        Date startDate = dateFormat.parse(defaultStartDate);
        //前一天23点
        Date endDate = dateFormat.parse(defaultEndDate);
        //起始时间戳,结束时间戳
        String courseReportQuery = "{\"startTime\":"+startDate.getTime()+",\"endTime\":"+endDate.getTime()+"}";
        //当前时间戳
        Long timestamp_ = new Date().getTime();
        String sign_ = this.createSign(interParm);
        JSONObject json = new JSONObject();
        json.put("appKey_", appKey_);
        json.put("courseReportQuery", courseReportQuery);
        json.put("timestamp_", timestamp_.toString());
        json.put("sign_", sign_);
        String ret = RestServiceUtils.post(URL + URLName,json.toJSONString() );
        JSONArray arr=JSONArray.parseArray(ret);
        List<Map<String,Object>> retlist=new ArrayList<>();
        for(Object item : arr){
            JSONObject mapitem=(JSONObject) item;
            retlist.add(mapitem);
        }
        return retlist;

    }
    @ServiceLog("Map时间戳转换服务")
    public List<Map<String,Object>> transMapKey2Value(List<Map<String,Object>> datas) {
        List<Map<String, Object>> newlist = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> data : datas) {
                Long date1 = (Long) data.get("学习完成时间");
                Date date = new Date(date1);
                data.put("学习完成时间", simpleDateFormat.format(date));
                data.put("部门",data.get("工号"));
                data.put("姓名",data.get("工号"));
                //data.put("部门","00190");
                newlist.add(data);
        }
        return newlist;
    }
    }


